package com.gomeplus.frame.template;

import freemarker.template.TemplateMethodModel;
import freemarker.template.TemplateModelException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;
/**
 * 
* @ClassName: JinduConfigTemplate 
* @Description: 计算  进度
* @author lixiaobin 
* @date 2015年3月25日 下午6:31:53
 */
public class JinduConfigTemplate implements TemplateMethodModel {

	private Logger logger = LoggerFactory.getLogger("JinduConfigTemplate");
	
	/**
	 * @param arguments .get(0) 融资金额
	 * @param arguments .get(1) 可购金额
	 */

	@Override
    @SuppressWarnings("rawtypes")
	public Object exec(List arguments) throws TemplateModelException {
		try{
			String packageIssueAmountStr = "";
			String packageLeftAmountStr = "";
			if(arguments.size()<=0){
				return 0;
			}else{
				packageIssueAmountStr = arguments.get(0).toString().replace(",", "");
				packageLeftAmountStr = arguments.get(1).toString().replace(",", "");
			}
			
			BigDecimal packageIssueAmount = new BigDecimal(packageIssueAmountStr);
			BigDecimal packageLeftAmount = new BigDecimal(packageLeftAmountStr);
			if(packageIssueAmount.compareTo(packageLeftAmount) == -1){
				return 100;
			}
			BigDecimal jindu =(packageIssueAmount.subtract(packageLeftAmount)).divide(packageIssueAmount,10,BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal("100"));
			return jindu.toBigInteger();
		}catch(Exception e){
			logger.error("计算进度格式化模板，计算进度异常：{}", e.getMessage());
		}
		return new BigInteger("0");
	}

}
